/*
 * @author java_deng
 * @date 2025/2/5 11:46
 * @description
 */
package org.hbwl.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.hbwl.dto.Region;
import org.hbwl.dto.User;
import org.hbwl.mapper.AnimalMapper;
import org.hbwl.mapper.PostMapper;
import org.hbwl.mapper.RegionMapper;
import org.hbwl.mapper.UserMapper;
import org.hbwl.po.UserPo;
import org.hbwl.utils.PageResponseData;
import org.hbwl.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private RegionService regionService;
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private AnimalMapper animalMapper;

    public UserVo login(User userDto) {

        UserVo user=userMapper.findByAccount(userDto.getAccount());
        if(user==null){
            return null;
        }
        if(!user.getPassword().equals(userDto.getPassword()))
        {
            return null;
        }
        return user;
    }

    public void save(User userDto) {
        Long addressId = userDto.getRegionId().get(2);
        if(addressId==0){
            addressId=userDto.getRegionId().get(1);
        }
        userDto.setAddressId(addressId);
        Region region = regionMapper.selectById(addressId);
        userDto.setAddress(region.getRegionName());
        String fullAddress=regionService.getFullAddress(region);
        userDto.setFullAddress(fullAddress);
        userDto.setAuth(0);
        userDto.setStatus(0);

        userMapper.insert(userDto);
    }



    public UserVo updateUserInfo(User userDto) {
        if(userDto.getRegionId()!=null){
            Long addressId = userDto.getRegionId().get(2);
            if(addressId==0){
                addressId=userDto.getRegionId().get(1);
            }
            userDto.setAddressId(addressId);
            Region region = regionMapper.selectById(addressId);
            userDto.setAddress(region.getRegionName());
            String fullAddress=regionService.getFullAddress(region);
            userDto.setFullAddress(fullAddress);
        }
        userMapper.updateById(userDto);
        UserVo res = userMapper.deail(userDto.getId());
        return res;
    }

    public Object getSystemInfo() {
        HashMap<String, Long> map = new HashMap<>();
        Long aLong = userMapper.selectCount(null);
        Long aLong1 = animalMapper.selectCount(null);
        Long aLong2 = postMapper.selectCount(null);
        map.put("userCount",aLong);
        map.put("animalCount",aLong1);
        map.put("postCount",aLong2);
        return map;
    }

    public Object managerList(UserPo userPo) {
        Page<UserVo> objects = PageHelper.startPage(userPo.getPageIndex(), userPo.getPageSize());
        userMapper.list(userPo);
        PageResponseData<UserVo> userPageResponseData = new PageResponseData<>();
        userPageResponseData.setResult(objects.getResult());
        userPageResponseData.setPageCount(objects.getPages());
        userPageResponseData.setItemCount(objects.getTotal());
        return userPageResponseData;
    }

    public void delete(Long id) {
        userMapper.deleteById(id);
    }
}
